package com.yunpuvip.sachet.biz.service.impl;

import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunpuvip.sachet.biz.entity.GiftInfo;
import com.yunpuvip.sachet.biz.mapper.GiftInfoMapper;
import com.yunpuvip.sachet.biz.model.params.GiftInfoParam;
import com.yunpuvip.sachet.biz.model.result.GiftInfoResult;
import  com.yunpuvip.sachet.biz.service.GiftInfoService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunpuvip.sachet.biz.service.PaymentService;
import com.yunpuvip.sachet.biz.service.SysparamService;
import com.yunpuvip.sachet.modular.constant.ApplicationConstant;
import com.yunpuvip.sachet.modular.core.exception.GunsException;
import com.yunpuvip.sachet.modular.core.exception.GunsExceptionEnum;
import com.yunpuvip.sachet.modular.page.LayuiPageFactory;
import com.yunpuvip.sachet.modular.page.LayuiPageInfo;
import com.yunpuvip.sachet.modular.util.BigDecimalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.ws.Action;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 赠送信息 服务实现类
 * </p>
 *
 * @author griftt
 * @since 2019-08-27
 */
@Service
public class GiftInfoServiceImpl extends ServiceImpl<GiftInfoMapper, GiftInfo> implements GiftInfoService {

    @Autowired
    private PaymentService paymentService;
    @Autowired
    private SysparamService sysparamService;

    @Override
    public void add(GiftInfoParam param){
        GiftInfo exGiftInfo = new GiftInfo();
        exGiftInfo.setAmountOne(param.getAmountOne());
        List<GiftInfo> giftInfo = getGiftInfo(exGiftInfo);
        if(giftInfo.size()>0){
            throw new ServiceException(GunsExceptionEnum.GIFT_EXIST);
        }
        GiftInfo entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(GiftInfoParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(GiftInfoParam param){
        GiftInfo oldEntity = getOldEntity(param);
        GiftInfo newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public GiftInfoResult findBySpec(GiftInfoParam param){

        return null;
    }

    @Override
    public List<GiftInfoResult> findListBySpec(GiftInfoParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(GiftInfoParam param){
        Page pageContext = getPageContext();
        QueryWrapper<GiftInfo> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.orderByDesc("amount_one","create_time");
        IPage page = this.page(pageContext, objectQueryWrapper);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(GiftInfoParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private GiftInfo getOldEntity(GiftInfoParam param) {
        return this.getById(getKey(param));
    }

    private GiftInfo getEntity(GiftInfoParam param) {
        GiftInfo entity = new GiftInfo();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    @Override
    public BigDecimal getGiftAmount(Integer playerId, BigDecimal payAmount,Boolean isRecharge) {
        //首次支付赠送
        if(paymentService.isFisrtPay(playerId)){
            String gift = sysparamService.getSysparamVlaue(ApplicationConstant.GIFTSCALE);
            BigDecimal giftAmout = new BigDecimal(gift);
            if(payAmount.compareTo(giftAmout)>0){
                return giftAmout.abs();
            }else {
                return payAmount ;
            }
        }

        //充值赠送
        if(isRecharge) {
            LambdaQueryWrapper<GiftInfo> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(GiftInfo::getAmountOne, payAmount);
            List<GiftInfo> giftInfos = baseMapper.selectList(wrapper);
            if (giftInfos.size() > 0) {
                return giftInfos.get(0).getGiftAmount();
            }
        }
        return BigDecimal.ZERO;
    }

    @Override
    public List<GiftInfo> getGiftInfo(GiftInfo giftInfo) {
        LambdaQueryWrapper<GiftInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GiftInfo::getRecordStatus,1);
        if(giftInfo!=null&&giftInfo.getAmountOne()!=null){
            wrapper.eq(GiftInfo::getAmountOne,giftInfo.getAmountOne());
        }
        List<GiftInfo> giftInfos = baseMapper.selectList(wrapper);
        return giftInfos;
    }
}
